@extends('layouts.admin')

@section('style')

@endsection

@section('content')
  
 
    <!-- Content Header (Page header) -->
    <section class="content">
    <div class="col-md-9" style="width: 100%">
          <div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                <li class="active"><a href="#activity" data-toggle="tab" aria-expanded="true">基本信息</a></li>
                <li class=""><a href="#timeline" data-toggle="tab" aria-expanded="false">其他信息</a></li>
                <li class=""><a href="#settings" data-toggle="tab" aria-expanded="false">sku</a></li>
                <li class=""><a href="#package" data-toggle="tab" aria-expanded="false">套餐</a></li>
            </ul>
            <form class="form-horizontal" action="{{route('admin.item.update')}}" method="POST" enctype="multipart/form-data">
            <div class="tab-content">
                <div class="tab-pane active" id="activity">
                
                {{ csrf_field() }}
                <input name='id' type="hidden" value="{{$item->id}}">
                    <div class="box-body">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">商品唯一货号</label>

                            <div class="col-sm-10">
                              <input type="text" class="form-control" name="sncode" id="inputEmail3" placeholder="商品唯一货号" value="{{$item->sncode}}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">商品名字</label>

                            <div class="col-sm-10">
                              <input type="text" class="form-control" name="name" id="inputEmail3" placeholder="商品名字" value="{{$item->name}}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">商品库存数量</label>

                            <div class="col-sm-10">
                             <input type="text" class="form-control" name="stock" id="inputEmail3" placeholder="商品库存数量" value="{{$item->stock}}">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">下单后锁定的数量</label>

                            <div class="col-sm-10">
                              <input type="text" class="form-control" name="locked" id="inputEmail3" placeholder="下单后锁定的数量" value="{{$item->locked}}">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">商品的重量</label>

                            <div class="col-sm-10">
                              <input type="text" class="form-control" name="weight" id="inputEmail3" placeholder="商品的重量，以千克为单位kg" value="{{$item->weight}}">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">市场售价</label>

                            <div class="col-sm-10">
                              <input type="text" class="form-control" name="market_price" id="market_price" placeholder="市场售价1.00" value="{{$item->market_price}}">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">本店售价</label>

                            <div class="col-sm-10">
                              <input type="text" class="form-control" name="price" id="price" placeholder="本店售价1.00" value="{{$item->price}}">
                            </div>
                        </div>

                        
                     
                    </div>
                
                </div>
              <!-- /.tab-pane -->
                <div class="tab-pane" id="timeline">
                
               
                    <div class="box-body">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">seo关键字</label>

                            <div class="col-sm-10">
                              <input type="text" class="form-control" name="keywords" id="inputEmail3" placeholder="seo关键字" value="{{$item->keywords}}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">简介</label>

                            <div class="col-sm-10">
                              <input type="text" class="form-control" name="brief" id="inputEmail3" placeholder="商品名字" value="{{$item->brief}}">
                            </div>
                        </div>

                        <div class="form-group">
                                <label for="inputEmail3" class="col-sm-2 control-label">商品详情描述</label>
                                <div class="col-sm-10">
                                    <textarea class="form-control" rows="3" name="description" placeholder="商品详情描述" >{{$item->description}}</textarea>
                                </div>
                        </div>

                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">商品图片</label>
                            <div class="col-sm-10">
                                <input  rows="3" type="file" name="cover" id="exampleInputFile" value="{{$item->cover}}">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">是否开放销售</label>
                            <div class="col-sm-10">
                               <input type="radio" name="is_onsale" value="1" @if($item->is_onsale==1) checked="checked"@endif>是 
                               <input type="radio" name="is_onsale" value="0" @if($item->is_onsale==0) checked="checked"@endif>否
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">是否能单独销售</label>
                            <div class="col-sm-10">
                               <input type="radio" name="is_alone_sale" value="1" @if($item->is_alone_sale==1) checked="checked"@endif>是 
                               <input type="radio" name="is_alone_sale" value="0" @if($item->is_alone_sale==0) checked="checked"@endif>否
                            </div>
                        </div>
                      

                        
                     
                    </div>
               
                </div>
              <!-- /.tab-pane -->

                <div class="tab-pane" id="settings">
                    <div class="box-body">
                        <div class="form-group " id="goods_type">
                            <label for="inputEmail3" class="col-sm-2 control-label">商品类型</label>
                            <div class="col-sm-10">
                                <select class="form-control" id="category" name="cat_id">
                                    <option value="">商品类型</option>
                                    @foreach($category as $v)
                                    <option value="{{$v['id']}}" @if($item->cat_id==$v['id']) selected="selected"@endif>{{$v['name']}}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>

                       

                        <div class="form-group">
                                <label for="inputEmail3" class="col-sm-2 control-label">sku</label>
                                <div class="col-sm-10">
                                    <table id="example2" class="table table-bordered table-hover">
                                        <thead>
                                        <tr>
                                            <th></th>
                                            <th>sku属性</th>
                                            <th>属性值</th>
                                            <th>价格</th>
                                            <th>数量</th>    
                                        </tr>
                                        </thead>
                                         <tbody id="tbody">
                                        @if($item->sku) 
                                        @foreach($item->sku as $v)
                                            <tr calss="tr">
                                            <input name="skuid[]" type="hidden" value="{{$v->id}}">
                                            <input name="skuidstr[]" type="hidden" value="{{$v->sku}}">
                                            <input name="skunamestr[]" type="hidden" value="{{$v->name}}">
                                            <td><input type="checkbox" ></td>               
                                            <td name="skunamestr" class="skunamestr"></td>
                                            <td>{{$v->name}}</td>
                                            <td style="width: 20%"><input type="text" class="form-control" name="skuprice[]" value="{{$v->price}}"></td>
                                            <td style="width: 20%"><input type="text" class="form-control" name="skustock[]" value="{{$v->stock}}"></td>
                                            </tr>

                                        @endforeach
                                        @endif
                    
                                        </tbody>
                                    </table> 
                                </div>
                        </div>

                    </div>
                </div>
                <div class="tab-pane" id="package">
                    <div class="box-body">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">是否是套餐</label>
                            <div class="col-sm-10">
                               <input type="radio" name="is_package" value="1" @if($item->is_package) checked="checked" @endif>是 
                               <input type="radio" name="is_package" value="0" @if(!$item->is_package) checked="checked" @endif>否
                            </div>
                        </div>

                        <div class="form-group  package" @if(!$item->is_package) style="display: none" @endif>
                                <label for="inputEmail3" class="col-sm-2 control-label">套餐</label>
                                <div class="col-sm-10">
                                    <table id="example2" class="table table-bordered table-hover">
                                        <thead>
                                        <tr>
                                            <th></th>
                                            <th>sku属性</th>
                                            <th>属性值</th>
                                            <th>描述</th>
                                            <th>数量</th>    
                                        </tr>
                                        </thead>
                                         <tbody id="tbody">
                                        @if($item->sku) 
                                        @foreach($item->sku as $v)
                                            <tr calss="tr package_list">
                                            <td>
                                                @if(inarray($v->id,$item->package)) 
                                                
                                                <input class="packagelist" name="package[]" type="hidden" value="{{getstr($v->id,$item->package)}}">
                                                @endif
                                                <input class="choose_package" type="checkbox" name="sku_id[]" value="{{$v->id}}" data-skuidstr="{{$v->sku}}" @if(inarray($v->id,$item->package)) checked="checked" @endif>
                                            </td>               
                                            <td name="skunamestr" class="skunamestr"></td>
                                            <td>{{$v->name}}</td>
                                            <td style="width: 20%"><input type="text" class="form-control" class="package_itemid" value="{!!get_package_itemid($v->id,$item->package)!!}"></td>
                                            <td style="width: 20%"><input type="text" class="form-control" class="number" value="{!!get_number($v->id,$item->package)!!}"></td>
                                            </tr>

                                        @endforeach
                                        @endif
                    
                                        </tbody>
                                    </table> 
                                </div>
                        </div>

                    </div>
                </div>
            
              <!-- /.tab-pane -->
                </div>
                <div class="box-footer">
                <button type="submit" class="btn btn-default" onClick="javascript:history.back()">返回</button>
                <button type="submit" class="btn btn-info pull-right ">提交</button>
              </div>
            </form>
            <!-- /.tab-content -->
            </div>
          <!-- /.nav-tabs-custom -->
    </div>
    <!-- /.content -->

    
    </section>

 


@endsection

@section('script')
    <script type="text/javascript">
        var sku={!!json_encode($item->sku,JSON_UNESCAPED_UNICODE )!!}
        var arr=[]
       
        var skuid=sku[0].sku
        arr=skuid.split(";");
        var attr=[]
        var a=[]
        for(var k in arr){
            a=arr[k].split(":");
            attr[k]=a[0]
        }
        
        

        $(function(){

            var cat_id= $("#category").val();
            var start=true
            chooseAttr(cat_id,start)
            return false;
        })



        $("#category").change(function(){
            var _this=$(this)
            if(_this.val()==""){
                return false;
            }
            chooseAttr(_this.val())
            
        })

        

        $(".tab-pane").on("click",".get-sku",function(){
            var str=""
            $.each($('input[name="attr_id"]:checked'),function(i){
               
                    if(i==0){
                        str=$(this).val()
                    }else{
                        str=str+","+$(this).val()
                    }
                     
            });
            attrlist(str)
        })


        $("input[name='is_package']").click(function(){
            var val=$(this).val()

            if(val==1){
                $(".package").show();
            }else{
                $(".package").hide();

            }
        })

        $(".choose_package").click(function(){
            if($(this).prop("checked")){  
                var skuid=$(this).val()
                var skuidstr=$(this).data('skuidstr')
                var package_itemid=$(this).parent().next().next().next().find('input').val()
                var number=$(this).parent().next().next().next().next().find('input').val()

                if(package_itemid=="" || number==""){
                    layer.alert('请填写套餐描述和数量')
                    $(this).prop("checked", false); 
                    return false;

                }
                var str=skuid+"|"+skuidstr+"|"+package_itemid+"|"+number
                var html='<input class="packagelist" name="package[]" type="hidden" value="'+str+'">'
                $(this).parent().prepend(html)
            }else{
            
               $(this).parent().find(".packagelist").remove()
            }
            
           
        })

        var chooseAttr=function(cat_id,start=""){
            $("#chooseAttr").html('')
            var url="{{route('admin.item.getAttributeName')}}?cat_id="+cat_id;
            $.get(url, {}, function(data){
                    var data=data.data
                    var str="";
                    
                        str+='<div class="form-group" id="chooseAttr">'
                        str+= '<label for="inputEmail3" class="col-sm-2 control-label">选择属性</label>'
                        str+=  ' <div class="col-sm-10">'
                        for(var k in data){  
                            str+= '<input type="checkbox"  name="attr_id" value="'+data[k].id+'">'+data[k].name+''
                        }
                        str+='<button type="button" class="btn btn-info get-sku">获取属性组合</button>'
                        str+=    '</div>'
                        str+='</div>'
                        $("#goods_type").after(str);

                        if(start){
                            var attr_str=""
                            $.each($('input[type="checkbox"][name="attr_id"]'),function(i){
                                var attr_id=$(this).val()
                                
                                for(var k in attr){
                                    if(attr_id==attr[k]){
                                        $(this).attr('checked','checked')
                                        attr_str=attr_str+data[k].name+","
                                        
                                    }
                                }
                                

                            });
                            
                            attr_str=attr_str.substring(0,attr_str.length-1);
                            $.each($('.skunamestr'),function(i){
                                $(this).text(attr_str)
                            })
                         
                        }
                    
            })
        }


        var attrlist=function(str){
            $("#tbody").html('')
            var url="{{route('admin.item.getAttribute')}}?attr_id="+str;
            $.get(url, {}, function(data){
                var tmp=data.data.tmp;
                var type=data.data.type;
                var sku=data.data.sku;
                var str=""
                var price=$("#price").val();
                var market_price=$("#market_price").val();
                for(var k in tmp){
                    
                    str+='<tr calss="tr">'
                    str+='<input name="skuidstr[]" type="hidden" value="'+sku[k]+'">'
                    str+='<input name="skunamestr[]" type="hidden" value="'+tmp[k]+'">'
                    str+='<td><input type="checkbox" ></td>'                 
                    str+='<td>'+type[k]+'</td>'
                    str+='<td>'+tmp[k]+'</td>'
                    str+='<td style="width: 20%"><input type="text" class="form-control" name="skuprice[]" value="'+price+'"></td>'
                    str+='<td style="width: 20%"><input type="text" class="form-control" name="skustock[]" value="100"></td>'
                    str+='</tr>'
                }
                
                $("#tbody").prepend(str);

            })
        }

    

    </script>
@endsection